package com.kyleswebspace.propertyanalyzer.gui;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.FocusEvent;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.TitledBorder;

import com.kyleswebspace.parser.FieldParser;
import com.kyleswebspace.propertyanalyzer.config.ArcaFont;
import com.kyleswebspace.propertyanalyzer.config.ColorBox;
import com.kyleswebspace.propertyanalyzer.model.DataModel;

public class LandXferTaxFrame extends JFrame implements Updateable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 6021889198624368484L;
	
	private DataModel dm;
	
	Color innerPanelColor = ColorBox.innerPanelColor;
	private JPanel bodyPanel;
	private JLabel exceedingLabelA, andUpToLabelA, equalsLabelAA, equalsLabelAB;
	private JLabel exceedingLabelB, andUpToLabelB, equalsLabelBA, equalsLabelBB;
	private JLabel exceedingLabelC, andUpToLabelC, equalsLabelCA, equalsLabelCB;
	private JLabel exceedingLabelD, andUpToLabelD, equalsLabelDA, equalsLabelDB, equalsLabelEB;
	private JTextField exceedingFieldA, andUpToFieldA, percentEqualFieldA, equalFieldA;
	private JTextField exceedingFieldB, andUpToFieldB, percentEqualFieldB, equalFieldB;
	private JTextField exceedingFieldC, andUpToFieldC, percentEqualFieldC, equalFieldC;
	private JTextField exceedingFieldD, percentEqualFieldD, equalFieldD;
	private JTextField totalField;
	
	Font appFont = ArcaFont.appFont;

	public LandXferTaxFrame(DataModel dm) {
		this.dm = dm;
		dm.register(this);
		
		bodyPanel = new JPanel();
		
		bodyPanel.setLayout(new GridBagLayout());
		TitledBorder title;
		title = BorderFactory.createTitledBorder("Land Transfer Tax (Residential Only)");
		bodyPanel.setBorder(title);
		bodyPanel.setBackground(innerPanelColor);
		
		GridBagConstraints constraints = new GridBagConstraints();
		constraints.ipadx = 20; //PADDING
		
		//init common labels
		exceedingLabelA = new JLabel("Exceeding");
		exceedingLabelB = new JLabel("Exceeding");
		exceedingLabelC = new JLabel("Exceeding");
		exceedingLabelD = new JLabel("Exceeding");
		andUpToLabelA = new JLabel("and up to");
		andUpToLabelB = new JLabel("and up to");
		andUpToLabelC = new JLabel("and up to");
		andUpToLabelD = new JLabel("and up to");
		equalsLabelAA = new JLabel ("=");
		equalsLabelAB = new JLabel ("=");
		equalsLabelBA = new JLabel ("=");
		equalsLabelBB = new JLabel ("=");
		equalsLabelCA = new JLabel ("=");
		equalsLabelCB = new JLabel ("=");
		equalsLabelDA = new JLabel ("=");
		equalsLabelDB = new JLabel ("=");
		equalsLabelEB = new JLabel ("=");
		exceedingLabelA.setFont(appFont);
		exceedingLabelB.setFont(appFont);
		exceedingLabelC.setFont(appFont);
		exceedingLabelD.setFont(appFont);
		exceedingLabelA.setHorizontalAlignment(JLabel.CENTER);
		exceedingLabelB.setHorizontalAlignment(JLabel.CENTER);
		exceedingLabelC.setHorizontalAlignment(JLabel.CENTER);
		exceedingLabelD.setHorizontalAlignment(JLabel.CENTER);
		andUpToLabelA.setFont(appFont);
		andUpToLabelB.setFont(appFont);
		andUpToLabelC.setFont(appFont);
		andUpToLabelD.setFont(appFont);
		andUpToLabelA.setHorizontalAlignment(JLabel.CENTER);
		andUpToLabelB.setHorizontalAlignment(JLabel.CENTER);
		andUpToLabelC.setHorizontalAlignment(JLabel.CENTER);
		andUpToLabelD.setHorizontalAlignment(JLabel.CENTER);
		equalsLabelAA.setFont(appFont);
		equalsLabelBA.setFont(appFont);
		equalsLabelBB.setFont(appFont);
		equalsLabelCA.setFont(appFont);
		equalsLabelCB.setFont(appFont);
		equalsLabelDA.setFont(appFont);
		equalsLabelDB.setFont(appFont);
		equalsLabelEB.setFont(appFont);
		equalsLabelAA.setHorizontalAlignment(JLabel.CENTER);
		equalsLabelAB.setHorizontalAlignment(JLabel.CENTER);
		equalsLabelBA.setHorizontalAlignment(JLabel.CENTER);
		equalsLabelBB.setHorizontalAlignment(JLabel.CENTER);
		equalsLabelCA.setHorizontalAlignment(JLabel.CENTER);
		equalsLabelCB.setHorizontalAlignment(JLabel.CENTER);
		equalsLabelDA.setHorizontalAlignment(JLabel.CENTER);
		equalsLabelDB.setHorizontalAlignment(JLabel.CENTER);
		equalsLabelEB.setHorizontalAlignment(JLabel.CENTER);
		
		//init fields
		exceedingFieldA = new JTextField(3);
		exceedingFieldB = new JTextField(3);
		exceedingFieldC = new JTextField(3);
		exceedingFieldD = new JTextField(3);
		exceedingFieldA.addFocusListener(this);
		exceedingFieldB.addFocusListener(this);
		exceedingFieldC.addFocusListener(this);
		exceedingFieldD.addFocusListener(this);
		exceedingFieldA.setFocusable(false);
		exceedingFieldB.setFocusable(false);
		exceedingFieldC.setFocusable(false);
		exceedingFieldD.setFocusable(false);
		exceedingFieldA.setFont(appFont); 
		exceedingFieldB.setFont(appFont); 
		exceedingFieldC.setFont(appFont); 
		exceedingFieldD.setFont(appFont);
		exceedingFieldA.setHorizontalAlignment(JTextField.RIGHT);
		exceedingFieldB.setHorizontalAlignment(JTextField.RIGHT);
		exceedingFieldC.setHorizontalAlignment(JTextField.RIGHT);
		exceedingFieldD.setHorizontalAlignment(JTextField.RIGHT);
		
		andUpToFieldA = new JTextField(3);
		andUpToFieldB = new JTextField(3);
		andUpToFieldC = new JTextField(3);
		andUpToFieldA.addFocusListener(this);
		andUpToFieldB.addFocusListener(this);
		andUpToFieldC.addFocusListener(this);
		andUpToFieldA.setFocusable(false);
		andUpToFieldB.setFocusable(false);
		andUpToFieldC.setFocusable(false);
		andUpToFieldA.setFont(appFont);
		andUpToFieldB.setFont(appFont);
		andUpToFieldC.setFont(appFont);
		andUpToFieldA.setHorizontalAlignment(JTextField.RIGHT);
		andUpToFieldB.setHorizontalAlignment(JTextField.RIGHT);
		andUpToFieldC.setHorizontalAlignment(JTextField.RIGHT);
		
		percentEqualFieldA = new JTextField(3);
		percentEqualFieldB = new JTextField(3);
		percentEqualFieldC = new JTextField(3);
		percentEqualFieldD = new JTextField(3);
		percentEqualFieldA.addFocusListener(this);
		percentEqualFieldB.addFocusListener(this);
		percentEqualFieldC.addFocusListener(this);
		percentEqualFieldD.addFocusListener(this);
		percentEqualFieldA.setFocusable(false);
		percentEqualFieldB.setFocusable(false);
		percentEqualFieldC.setFocusable(false);
		percentEqualFieldD.setFocusable(false);
		percentEqualFieldA.setFont(appFont);
		percentEqualFieldB.setFont(appFont);
		percentEqualFieldC.setFont(appFont);
		percentEqualFieldD.setFont(appFont);
		percentEqualFieldA.setHorizontalAlignment(JTextField.RIGHT);
		percentEqualFieldB.setHorizontalAlignment(JTextField.RIGHT);
		percentEqualFieldC.setHorizontalAlignment(JTextField.RIGHT);
		percentEqualFieldD.setHorizontalAlignment(JTextField.RIGHT);
		
		equalFieldA = new JTextField(3);
		equalFieldB = new JTextField(3);
		equalFieldC = new JTextField(3);
		equalFieldD = new JTextField(3);
		equalFieldA.addFocusListener(this);
		equalFieldB.addFocusListener(this);
		equalFieldC.addFocusListener(this);
		equalFieldD.addFocusListener(this);
		equalFieldA.setFocusable(false);
		equalFieldB.setFocusable(false);
		equalFieldC.setFocusable(false);
		equalFieldD.setFocusable(false);
		equalFieldA.setFont(appFont);
		equalFieldB.setFont(appFont);
		equalFieldC.setFont(appFont);
		equalFieldD.setFont(appFont);
		equalFieldA.setHorizontalAlignment(JTextField.RIGHT);
		equalFieldB.setHorizontalAlignment(JTextField.RIGHT);
		equalFieldC.setHorizontalAlignment(JTextField.RIGHT);
		equalFieldD.setHorizontalAlignment(JTextField.RIGHT);
		
		totalField = new JTextField(3);
		totalField.addFocusListener(this);
		totalField.setFocusable(false);
		totalField.setFont(appFont);
		totalField.setHorizontalAlignment(JTextField.RIGHT);
		
		
		//Row A
		bodyPanel.add(exceedingLabelA, constraints);
		constraints.gridx = 1;
		bodyPanel.add(exceedingFieldA, constraints);
		constraints.gridx = 2;
		bodyPanel.add(andUpToLabelA, constraints);
		constraints.gridx = 3;
		bodyPanel.add(andUpToFieldA, constraints);
		constraints.gridx = 4;
		bodyPanel.add(equalsLabelAA, constraints);
		constraints.gridx = 5;
		bodyPanel.add(percentEqualFieldA, constraints);
		constraints.gridx = 6;
		bodyPanel.add(equalsLabelAB, constraints);
		constraints.gridx = 7;
		bodyPanel.add(equalFieldA, constraints);
		
		//Row B
		constraints.gridy = 1;
		constraints.gridx = 0;
		bodyPanel.add(exceedingLabelB, constraints); 
		constraints.gridx = 1;
		bodyPanel.add(exceedingFieldB, constraints);
		constraints.gridx = 2;
		bodyPanel.add(andUpToLabelB, constraints);
		constraints.gridx = 3;
		bodyPanel.add(andUpToFieldB, constraints);
		constraints.gridx = 4;
		bodyPanel.add(equalsLabelBA, constraints);
		constraints.gridx = 5;
		bodyPanel.add(percentEqualFieldB, constraints);
		constraints.gridx = 6;
		bodyPanel.add(equalsLabelBB, constraints);
		constraints.gridx = 7;
		bodyPanel.add(equalFieldB, constraints);
		
		//Row C
		constraints.gridy = 2;
		constraints.gridx = 0;
		bodyPanel.add(exceedingLabelC, constraints); 
		constraints.gridx = 1;
		bodyPanel.add(exceedingFieldC, constraints);
		constraints.gridx = 2;
		bodyPanel.add(andUpToLabelC, constraints);
		constraints.gridx = 3;
		bodyPanel.add(andUpToFieldC, constraints);
		constraints.gridx = 4;
		bodyPanel.add(equalsLabelCA, constraints);
		constraints.gridx = 5;
		bodyPanel.add(percentEqualFieldC, constraints);
		constraints.gridx = 6;
		bodyPanel.add(equalsLabelCB, constraints);
		constraints.gridx = 7;
		bodyPanel.add(equalFieldC, constraints);
		
		//Row D
		constraints.gridy = 3;
		constraints.gridx = 0;
		bodyPanel.add(exceedingLabelD, constraints); 
		constraints.gridx = 1;
		bodyPanel.add(exceedingFieldD, constraints);
		constraints.gridx = 4;
		bodyPanel.add(equalsLabelDA, constraints);
		constraints.gridx = 5;
		bodyPanel.add(percentEqualFieldD, constraints);
		constraints.gridx = 6;
		bodyPanel.add(equalsLabelDB, constraints);
		constraints.gridx = 7;
		bodyPanel.add(equalFieldD, constraints);
		
		//separator
		constraints.gridx = 0;
		constraints.gridwidth = 8;
		constraints.gridy = 4;
		constraints.fill = GridBagConstraints.HORIZONTAL;
		bodyPanel.add(new JSeparator(SwingConstants.HORIZONTAL),constraints);
		constraints.fill = GridBagConstraints.NONE;
		constraints.gridwidth = 1;
		
		//total
		constraints.gridy = 5;
		constraints.gridx = 6;
		bodyPanel.add(equalsLabelEB, constraints);
		constraints.gridx = 7;
		bodyPanel.add(totalField, constraints);
		
		this.add(bodyPanel);
		this.pack();
		this.setTitle("Land Transfer Tax (Ontario)");
		this.setLocationRelativeTo(null);
		
		updateFields();
	}
	
	public void updateFields() {
		this.exceedingFieldA.setText(dm.getTransferTax1A());
		this.andUpToFieldA.setText(dm.getTransferTax1B());
		this.percentEqualFieldA.setText(dm.getTransferTax1C() + "%");
		this.equalFieldA.setText(dm.getTransferTax1D());
		
		this.exceedingFieldB.setText(dm.getTransferTax2A());
		this.andUpToFieldB.setText(dm.getTransferTax2B());
		this.percentEqualFieldB.setText(dm.getTransferTax2C() + "%");
		this.equalFieldB.setText(dm.getTransferTax2D());
		
		this.exceedingFieldC.setText(dm.getTransferTax3A());
		this.andUpToFieldC.setText(dm.getTransferTax3B());
		this.percentEqualFieldC.setText(dm.getTransferTax3C() + "%");
		this.equalFieldC.setText(dm.getTransferTax3D());
		
		this.exceedingFieldD.setText(dm.getTransferTax4A());
		this.percentEqualFieldD.setText(dm.getTransferTax4C() + "%");
		this.equalFieldD.setText(dm.getTransferTax4D());
		
		this.totalField.setText(dm.getTransferTaxTotal());
	}

	@Override
	public void focusGained(FocusEvent e) {
		
	}

	@Override
	public void focusLost(FocusEvent e) {
		dm.setTransferTax1A(FieldParser.parseInteger(this.exceedingFieldA.getText()));
		dm.setTransferTax1B(FieldParser.parseInteger(this.andUpToFieldA.getText()));
		dm.setTransferTax1C(FieldParser.parseDecimal(this.percentEqualFieldA.getText()));
		
		dm.setTransferTax2A(FieldParser.parseInteger(this.exceedingFieldB.getText()));
		dm.setTransferTax2B(FieldParser.parseInteger(this.andUpToFieldB.getText()));
		dm.setTransferTax2C(FieldParser.parseDecimal(this.percentEqualFieldB.getText()));
		
		dm.setTransferTax3A(FieldParser.parseInteger(this.exceedingFieldC.getText()));
		dm.setTransferTax3B(FieldParser.parseInteger(this.andUpToFieldC.getText()));
		dm.setTransferTax3C(FieldParser.parseDecimal(this.percentEqualFieldC.getText()));
		
		dm.setTransferTax4A(FieldParser.parseInteger(this.exceedingFieldD.getText()));
		dm.setTransferTax4C(FieldParser.parseDecimal(this.percentEqualFieldD.getText()));
		
		dm.updateInterface();
	}
}
